import App from "./App";

// #ifndef VUE3
import Vue from "vue";
Vue.config.productionTip = false;
App.mpType = "app";
const app = new Vue({
  ...App,
});

//注册uView组件
import uView from "uview-ui";
Vue.use(uView);

app.$mount();
// #endif

// #ifdef VUE3
import { createSSRApp } from "vue";
export function createApp() {
  const app = createSSRApp(App);
  return {
    app,
  };
}
// #endif
// hxds-customer
let baseUrl = "https://jd.zdwkj.com/bff-customer";

Vue.prototype.url = {
  registerNewCustomer: `${baseUrl}/customer/registerNewCustomer`,
  login: `${baseUrl}/customer/login`,
  insertCustomerCar: `${baseUrl}/customer/car/insertCustomerCar`,
  searchCustomerCarList: `${baseUrl}/customer/car/searchCustomerCarList`,
  deleteCustomerCarById: `${baseUrl}/customer/car/deleteCustomerCarById`,
  createNewOrder: `${baseUrl}/order/createNewOrder`,
  searchOrderStatus: `${baseUrl}/order/searchOrderStatus`,
  deleteUnAcceptOrder: `${baseUrl}/order/deleteUnAcceptOrder`,
  hasCustomerCurrentOrder: `${baseUrl}/order/hasCustomerCurrentOrder`,
  searchOrderForMoveById: `${baseUrl}/order/searchOrderForMoveById`,
  searchOrderLocationCache: `${baseUrl}/order/location/searchOrderLocationCache`,
  confirmArriveStartPlace: `${baseUrl}/order/confirmArriveStartPlace`,
  receiveBillMessage: `${baseUrl}/message/receiveBillMessage`,
  searchOrderById: `${baseUrl}/order/searchOrderById`,
  createWxPayment: `${baseUrl}/order/createWxPayment`,
  updateOrderAboutPayment: `${baseUrl}/order/updateOrderAboutPayment`,
  insertComment: `${baseUrl}/comment/insertComment`,
  searchCustomerOrderByPage: `${baseUrl}/order/searchCustomerOrderByPage`,
  searchUnTakeVoucherByPage: `${baseUrl}/voucher/searchUnTakeVoucherByPage`,
  searchUnUseVoucherByPage: `${baseUrl}/voucher/searchUnUseVoucherByPage`,
  searchUsedVoucherByPage: `${baseUrl}/voucher/searchUsedVoucherByPage`,
  searchUnUseVoucherCount: `${baseUrl}/voucher/searchUnUseVoucherCount`,
  takeVoucher: `${baseUrl}/voucher/takeVoucher`,
};

Vue.prototype.tencent = {
  map: {
    referer: "嘿代驾",
    // key: "WSMBZ-VSAKM-Q2R64-6UTJ6-XKURF-SBBRM",
    key: "BDHBZ-AUI6Q-M475G-BS5C3-U3UCF-VNFON",
  },
};

Vue.prototype.ajax = function (url, method, data, fun, load,tipmsg="加载中") {
  let timer = null;
  if (load == true || load == undefined) {
    uni.showLoading({
      title: tipmsg,
    });
    timer = setTimeout(function () {
      uni.hideLoading();
    }, 60 * 1000);
  }

  uni.request({
    url: url,
    method: method,
    header: {
      token: uni.getStorageSync("token"),
    },
    data: data,
    success: function (resp) {
      if (load == true || load == undefined) {
        clearTimeout(timer);
        uni.hideLoading();
      }
      if (resp.statusCode == 401) {
        uni.redirectTo({
          url: "/pages/login/login.vue",
        });
      } else if (resp.statusCode == 200 && resp.data.code == 200) {
        let data = resp.data;
        if (data.hasOwnProperty("token")) {
          let token = data.token;
          uni.setStorageSync("token", token);
        }
        fun(resp);
      } else {
        uni.showToast({
          icon: "none",
          title: "执行异常",
        });
        console.error(resp.data);
      }
    },
    fail: function (error) {
      if (load == true || load == undefined) {
        clearTimeout(timer);
        uni.hideLoading();
      }
    },
  });
};

Vue.prototype.refreshMessage = function (that) {
  uni.request({
    url: that.url.refreshMessage,
    method: "POST",
    header: {
      token: uni.getStorageSync("token"),
    },
    data: {
      identity: "driver",
    },
    success: function (resp) {
      if (resp.statusCode == 401) {
        uni.redirectTo({
          url: "/pages/login/login.vue",
        });
      } else if (resp.statusCode == 200 && resp.data.code == 200) {
        uni.$emit("updateMessageService", true);
        let result = resp.data.result;
        let lastRows = result.lastRows;
        let unreadRows = result.unreadRows;
        if (lastRows > 0) {
          uni.$emit("showMessageTip", lastRows);
        }
      } else {
        console.error(resp.data);
        //在工作台页面触发更新消息服务状态，显示服务可用或者不可用
        uni.$emit("updateMessageService", false);
      }
      console.log("刷新消息");
    },
    fail: function (error) {
      //在工作台页面触发更新消息服务状态，显示服务可用或者不可用
      uni.$emit("updateMessageService", false);
    },
  });
};

Vue.prototype.uploadCos = function (url, path, module, fun) {
  uni.uploadFile({
    url: url,
    filePath: path,
    name: "file",
    header: {
      token: uni.getStorageSync("token"),
    },
    formData: {
      module: module,
    },
    success: function (resp) {
      let data = JSON.parse(resp.data);
      if (resp.statusCode == 401) {
        uni.redirectTo({
          url: "/pages/login/login.vue",
        });
      } else if (resp.statusCode == 200 && data.code == 200) {
        fun(resp);
      } else {
        uni.showToast({
          icon: "none",
          title: data.error,
        });
      }
    },
  });
};

Vue.prototype.upload = function (url, path, data, fun) {
  uni.uploadFile({
    url: url,
    filePath: path,
    name: "file",
    header: {
      token: uni.getStorageSync("token"),
    },
    formData: data,
    success: function (resp) {
      let data = JSON.parse(resp.data);
      if (resp.statusCode == 401) {
        uni.redirectTo({
          url: "/pages/login/login.vue",
        });
      } else if (resp.statusCode == 200 && data.code == 200) {
        fun(resp);
      } else {
        uni.showToast({
          icon: "none",
          title: data.error,
        });
      }
    },
  });
};

Vue.prototype.toPage = function (url) {
  uni.navigateTo({
    url: url,
  });
};

Vue.prototype.checkNull = function (data, name) {
  if (data == null) {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return true;
  }
  return false;
};

Vue.prototype.checkBlank = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return true;
  }
  return false;
};

Vue.prototype.checkValidName = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (!/^[\u4e00-\u9fa5]{2,15}$/.test(data)) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.checkValidTel = function (data, name) {
	// let telReg=/^1[0-9]{10}$/
  let telReg= /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      // type: "error",
    });
    return false;
  } else if (!telReg.test(data)) {
    this.$refs.uToast.show({
      title: name + "不正确",
      // type: "error",
    });
    return false;
  }
  return true;
};
Vue.prototype.checkValidEmail = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (
    !/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(data)
  ) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.checkValidAddress = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (!/^[0-9a-zA-Z\u4e00-\u9fa5\-]{6,50}$/.test(data)) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.checkValidFee = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (!/^[1-9]\d*\.\d{1,2}$|^0\.\d{1,2}$|^[1-9]\d*$/.test(data)) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.checkValidCarType = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (!/^[\u4e00-\u9fa5A-Za-z0-9\-\_\s]{2,20}$/.test(data)) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.checkValidCarPlate = function (data, name) {
  if (data == null || data == "") {
    this.$refs.uToast.show({
      title: name + "不能为空",
      type: "error",
    });
    return false;
  } else if (
    !/^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$/.test(
      data
    )
  ) {
    this.$refs.uToast.show({
      title: name + "不正确",
      type: "error",
    });
    return false;
  }
  return true;
};

Vue.prototype.changeNumber = function (value) {
  let newValue = ["", ""];
  let fr = 1000;
  const ad = 1;
  let num = 3;
  const fm = 1;
  while (value / fr >= 1) {
    fr *= 10;
    num += 1;
  }
  if (num <= 4) {
    // 千
    newValue[1] = "千";
    newValue[0] = parseInt(value / 1000) + "";
  } else if (num <= 8) {
    // 万
    const text1 = parseInt(num - 4) / 3 > 1 ? "千万" : "万";
    const fm = "万" === text1 ? 10000 : 10000000;
    newValue[1] = text1;
    newValue[0] = value / fm + "";
  } else if (num <= 16) {
    // 亿
    let text1 = (num - 8) / 3 > 1 ? "千亿" : "亿";
    text1 = (num - 8) / 4 > 1 ? "万亿" : text1;
    text1 = (num - 8) / 7 > 1 ? "千万亿" : text1;
    // tslint:disable-next-line:no-shadowed-variable
    let fm = 1;
    if ("亿" === text1) {
      fm = 100000000;
    } else if ("千亿" === text1) {
      fm = 100000000000;
    } else if ("万亿" === text1) {
      fm = 1000000000000;
    } else if ("千万亿" === text1) {
      fm = 1000000000000000;
    }
    newValue[1] = text1;
    newValue[0] = parseInt(value / fm) + "";
  }
  if (value < 1000) {
    newValue[1] = "";
    newValue[0] = value + "";
  }
  let temp = Math.floor(newValue[0] * 100) / 100;
  return temp + newValue[1];
};
